package com.springboot.controller.blog;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.config.controller.BaseController;
import com.springboot.returndata.dto.blog.BlogCloudTagAddUpDto;
import com.springboot.returndata.dto.blog.BlogCloudTagPageDto;
import com.springboot.returndata.vo.blog.BlogCloudTagVo;
import com.springboot.service.blog.BlogCloudTagService;
import com.springboot.util.enums.RespErrorCode;
import com.springboot.util.http.ResponseBuilder;
import com.springboot.util.http.ResultResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 博客标签 前端控制器
 * </p>
 *
 * @author zyj
 * @since 2023-03-23
 */
@RestController
@RequestMapping("/blogCloudTag")
@Slf4j
@Tag(name = "博客管理标签")
public class BlogCloudTagController extends BaseController {

    @Resource
    private BlogCloudTagService blogCloudTagService;

    @PostMapping("/pageBlogTag")
    @Operation(description = "获取文章标签列表")
    public ResultResponse<Page<BlogCloudTagVo>> pageBlogTag(@RequestBody BlogCloudTagPageDto blogCloudTagPageDto){
        try {
            Page<BlogCloudTagVo> blogCloudTagVoPage = blogCloudTagService.pageBlogTag(blogCloudTagPageDto);
            return ResponseBuilder.successData(blogCloudTagVoPage);
        }catch (Exception e){
            log.error("pageBlogTag error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());


    }

    @GetMapping(value = "/listBlogTag")
    @Operation(description = "获取文章标签所有列表")
    public ResultResponse<BlogCloudTagVo> listBlogTagAll(){
        try {
            List<BlogCloudTagVo> blogSort = blogCloudTagService.listBlogSortAll();
            return ResponseBuilder.successData(blogSort);
        }catch (Exception e){
            log.error("listBlogTagAll error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.GET_INFO_NULL.getValue(), RespErrorCode.GET_INFO_NULL.getDescripe());

    }

    @PostMapping("/addBlogTag")
    @Operation(description = "添加文章标签")
    public ResultResponse<String> addBlogTag(@RequestBody BlogCloudTagAddUpDto blogCloudTagAddUpDto){
        try {
            blogCloudTagService.addBlogTag(blogCloudTagAddUpDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("addBlogTag error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.SAVE_ERROR.getValue(), RespErrorCode.SAVE_ERROR.getDescripe());


    }


    @PostMapping("/updateBlogTag")
    @Operation(description = "修改文章标签")
    public ResultResponse<String> updateBlogTag(@RequestBody BlogCloudTagAddUpDto blogCloudTagAddUpDto){
        try {
            blogCloudTagService.updateBlogTag(blogCloudTagAddUpDto);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("updateBlogTag error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.UPDATE_ERROR.getValue(), RespErrorCode.UPDATE_ERROR.getDescripe());


    }

    @GetMapping("/deleteBlogTag")
    @Operation(description = "根据id删除文章标签")
    public ResultResponse<String> deleteBlogTag(@RequestParam Integer id){
        try {
            blogCloudTagService.deleteBlogTag(id);
            return ResponseBuilder.success();
        }catch (Exception e){
            log.error("deleteBlogTag error", e);
        }
        return ResponseBuilder.errorMessage(RespErrorCode.DELETE_ERROR.getValue(), RespErrorCode.DELETE_ERROR.getDescripe());


    }
}

